<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <canvas id="c1" width="600" height="400">
        当前浏览器不支持canvas，请下载最新的浏览器
        <a href="https://www.google.cn/intl/zh-CN/chrome/">立即下载</a>
    </canvas>
    
    <script>
        /** @type {HTMLCanvasElement} */
        //1.找到画布
        var c1 = document.getElementById("c1");

        //判断是否有context
        if(!c1.getContext){
            console.log("当前浏览器不支持canvas，请下载最新的浏览器")
        }
        //2.获取画笔，上下文对象
        var ctx = c1.getContext("2d");

        let index = 0;

        function render(){
            ctx.clearRect(0,0,600,400); 
            index += 0.01;
            if(index>1){
                index=0;
            }
            let linearGradient=ctx.createLinearGradient(0,0,600,400);
            linearGradient.addColorStop(0,"red");
            linearGradient.addColorStop(index,"#ffcc66");
            linearGradient.addColorStop(1,"blue");
            ctx.fillStyle = linearGradient;
            
            ctx.fillRect(0,0,600,400); 
            requestAnimationFrame(render);

        }

        requestAnimationFrame(render);



    </script>
</body>
</html>